<!--
用户表、investor表的数据都要进行匹配。
1.用户id 和investor id 都通过username来查询得到
-->
<template>
<div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="运营公司名称" prop="companyName">
            <el-input v-model="queryParams.companyName" placeholder="请输入运营公司名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>

        <el-form-item label="微信电话" prop="wxTel">
            <el-input v-model="queryParams.wxTel" placeholder="请输入微信电话" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>

        <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['scene:scenemember:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
            <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['scene:scenemember:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['scene:scenemember:remove']">删除</el-button>
        </el-col>
        <el-col :span="1.5">
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['scene:scenemember:export']">导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scenememberList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" prop="id" />
        <!-- <el-table-column label="申请时填入的景区名称" align="center" prop="sceneCasualName" /> -->
        <!-- <el-table-column label="用户表id" align="center" prop="userId" />
      <el-table-column label="投资人id" align="center" prop="investorId" /> -->
        <!-- <el-table-column label="景区基础表id" align="center" prop="sceneBaseId" /> -->
        <el-table-column label="运营公司名称" align="center" prop="companyName" />
        <el-table-column label="联系人" align="center" prop="linkName" />
        <el-table-column label="联系电话" align="center" prop="linkTel" />
        <el-table-column label="微信昵称" align="center" prop="wxNickname" />
        <el-table-column label="微信电话" align="center" prop="wxTel" />
        <!-- <el-table-column label="营业执照" align="center" prop="licensePic" >
        <template slot-scope="scope">
          <el-popover placement="top-start" trigger="click">
            <a :href="scope.row.licensePic" target="_blank" title="查看图片">
              <img :src="scope.row.licensePic" style="width: 500px;height: 400px">
            </a>
            <img slot="reference" :src="scope.row.licensePic" style="width: 50px;height: 50px; cursor:pointer">
          </el-popover>
        </template>
      </el-table-column> /> -->
        <el-table-column label="管理人员" align="center" prop="managerInvestor" />
        <!-- <el-table-column label="关联景区" align="center" prop="sceneBaseName" /> -->
        <!-- <el-table-column label="关联景区报告" align="center" prop="linkReport" /> -->
        <el-table-column label="注册时间" align="center" prop="registerTime" width="180">
            <template slot-scope="scope">
                <span>{{ parseTime(scope.row.registerTime, "{y}-{m}-{d}") }}</span>
            </template>
        </el-table-column>
        <el-table-column label="认证状态" align="center" prop="sceneAuthStatus">
            <template slot-scope="scope">
                <dict-tag :options="dict.type.member_auth_status" :value="scope.row.sceneAuthStatus" />
            </template> </el-table-column>/>
        <el-table-column label="是否付费" align="center" prop="isVip" width="180">
            <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isVip" />
            </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleAuditPop(scope.row)" v-hasPermi="['scene:scenemember:edit']">认证</el-button>
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['scene:scenemember:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['scene:scenemember:remove']">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    <el-dialog title="审批景区运营公司" :visible.sync="open2" width="30%">
        <span>若同意审批或拒绝审批请选择，若暂时不选择请点击取消。</span>
        <span slot="footer" class="dialog-footer">
            <el-button @click="open2 = false">取 消</el-button>
            <el-button type="warning" @click="prohibitAudit">拒 绝</el-button>
            <el-button type="primary" @click="proceedAudit">批 准</el-button>
        </span>
    </el-dialog>

    <!-- 添加或修改景区注册公司对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <!-- <el-form-item label="用户表id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户表id" />
        </el-form-item>
        <el-form-item label="投资人id" prop="investorId">
          <el-input v-model="form.investorId" placeholder="请输入投资人id" />
        </el-form-item>
        <el-form-item label="景区基础表id" prop="sceneBaseId">
          <el-input v-model="form.sceneBaseId" placeholder="请输入景区基础表id" />
        </el-form-item> -->
            <template v-if="addFlag == false">
                <el-form-item label="申请时景区名称" prop="sceneCasualName">
                    <el-input v-model="form.sceneCasualName" placeholder="请输入景区公司申请时填入的景区名称" />
                </el-form-item>
            </template>
            <el-form-item label="运营公司名称" prop="companyName">
                <el-input v-model="form.companyName" placeholder="请输入运营公司名称" />
            </el-form-item>
            <el-form-item label="联系人" prop="linkName">
                <el-input v-model="form.linkName" placeholder="请输入联系人" />
            </el-form-item>
            <el-form-item label="联系电话" prop="linkTel">
                <el-input v-model="form.linkTel" placeholder="请输入联系电话" />
            </el-form-item>
            <el-form-item label="微信昵称" prop="wxNickname">
                <el-input v-model="form.wxNickname" placeholder="请输入微信昵称" />
            </el-form-item>
            <el-form-item label="微信电话" prop="wxTel">
                <el-input v-model="form.wxTel" placeholder="请输入微信电话" />
            </el-form-item>
            <!-- <el-form-item label="营业执照" prop="licensePic">
          <el-input v-model="form.licensePic" placeholder="请输入营业执照" />
        </el-form-item> -->
            <el-form-item label="管理人员" prop="managerInvestor">
                <el-input v-model="form.managerInvestor" placeholder="请输入管理人员" />
            </el-form-item>
            <!-- <el-form-item label="关联景区" prop="linkScenic">
               
                <template>
                    <el-select v-model="form.sceneBaseId" filterable placeholder="请选择景区" @blur="selectScene">
                        <el-option v-for="item in scenebaseList" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item> -->

            <!-- <el-form-item label="注册时间" prop="registerTime">
          <el-date-picker clearable
            v-model="form.registerTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择注册时间">
          </el-date-picker>
        </el-form-item>
         -->
            <el-form-item label="是否付费" prop="isVip">
                <el-select v-model="form.isVip" placeholder="请选择">
                    <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</div>
</template>

<script>
import {
    listScenemember,
    getScenemember,
    delScenemember,
    addScenemember,
    addScenememberByAdmin,
    updateScenemember,
    auditScenemember,
} from "@/api/scene/scenemember";
import {
    listInvestor
} from "@/api/system/investor";
import {
    listScenebase
} from "@/api/scenebase/scenebase";

export default {
    name: "Scenemember",
    dicts: ["sys_yes_no", "member_auth_status"],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 子表选中数据
            checkedSysUser: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 景区注册公司表格数据
            scenememberList: [],
            // 用户信息表格数据
            sysUserList: [],
            scenebaseList: [],
            addFlag: false, //添加还是修改，false为添加，true为修改
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            open2: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                userId: null,
                investorId: null,
               // sceneBaseId: null,
                companyName: null,
                linkName: null,
                linkTel: null,
                wxNickname: null,
                wxTel: null,
                licensePic: null,
                managerInvestor: null,
                linkScenic: null,
                linkReport: null,
                registerTime: null,
                modifyTime: null,
                sceneCasualName: null,
            },
            queryParamsSceneBase: {
                pageNum: 1,
                pageSize: 10000,
                name: null,
                addrProvince: null,
                addrCity: null,
                ifAvailable: null,
                ifPaid: null,
                sortId: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                wxTel: [{
                    required: true,
                    message: "微信电话不能为空",
                    trigger: "blur",
                }, ],
            },
        };
    },
    created() {
        this.getList();
    },
    watch: {
        open(newVal, oldVal) {
            console.log('打开新增或修改：' + newVal + ',' + oldVal)
            //监控单个变量
            // if (newVal == true) {
            //     listScenebase(this.queryParamsSceneBase).then((response) => {
            //         this.scenebaseList = response.rows;
            //     });
            // }
        },
    },
    methods: {
        selectScene(e) {
            console.log('输入景区名称:' + e.target.value)
        },
        /** 查询景区注册公司列表 */
        getList() {
            this.loading = true;
            listScenemember(this.queryParams).then((response) => {
                this.scenememberList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
            // listScenebase(this.queryParamsSceneBase).then((response) => {
            //     this.scenebaseList = response.rows;
            // });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                userId: null,
                investorId: null,
               // sceneBaseId: null,
                companyName: null,
                linkName: null,
                linkTel: null,
                wxNickname: null,
                wxTel: null,
                licensePic: null,
                managerInvestor: null,
                linkScenic: null,
                linkReport: null,
                registerTime: null,
                modifyTime: null,
                sceneCasualName: null,
            };
            this.sysUserList = [];
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.addFlag = true;
            this.open = true;
            this.title = "添加景区注册公司";
        },
        /**进行认证操作 auditScenemember*/
        handleAuditPop(row) {
            this.reset();
            const id = row.id;
            if (id != null) {
                getScenemember(id).then((response) => {
                    this.form = response.data;
                    this.open2 = true;
                    this.title = "景区运营公司认证";
                });
            }
        },

        proceedAudit() {
            this.form.isAuth = "Y";
            let that = this;
            auditScenemember(this.form).then((response) => {
                that.$modal.msgSuccess("审核成功");
                that.open2 = false;
                that.getList();
            });
        },

        prohibitAudit() {
            this.form.isAuth = "N";
            let that = this;
            auditScenemember(this.form).then((response) => {
                that.$modal.msgSuccess("审批拒绝");
                that.open2 = false;
                that.getList();
            });
        },

        /** 修改按钮操作 */
        handleUpdate(row) {
            let that = this;
            this.reset();
            this.addFlag = false;
            const id = row.id || this.ids;
            getScenemember(id).then((response) => {
                that.form = response.data;
                that.form.sceneBaseId = that.form.sceneBaseId - 0;
                that.open = true;
                that.title = "修改景区注册公司";
            });
        },
        /** 提交按钮 */
        submitForm() {
            // 通过wxtel 即用户名来获取userid和investerid，从而写入form
            let queryParam2 = {
                pageNum: 1,
                pageSize: 10,
                userName: this.form.wxTel,
            };
            let that = this;
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    this.form.sysUserList = this.sysUserList;
                    if (this.form.id != null) {
                        updateScenemember(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        listInvestor(queryParam2).then((res) => {
                            let inv_retval = res.rows;
                            if (inv_retval == null || inv_retval.length == 0) {
                                that.$modal.msgError("用户账号不存在，不允许添加");
                                return;
                            }
                            this.form.userId = res.rows[0].userId;
                            this.form.investorId = res.rows[0].id;
                            addScenememberByAdmin(this.form).then((response) => {
                                that.$modal.msgSuccess("新增成功");
                                that.open = false;
                                that.getList();
                            });
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm('是否确认删除景区注册公司编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delScenemember(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {});
        },
        /** 用户信息序号 */
        rowSysUserIndex({
            row,
            rowIndex
        }) {
            row.index = rowIndex + 1;
        },
        /** 用户信息添加按钮操作 */
        handleAddSysUser() {
            let obj = {};
            obj.deptId = "";
            obj.userName = "";
            obj.nickName = "";
            obj.userType = "";
            obj.email = "";
            obj.phonenumber = "";
            obj.sex = "";
            obj.avatar = "";
            obj.password = "";
            obj.status = "";
            obj.loginIp = "";
            obj.loginDate = "";
            obj.remark = "";
            this.sysUserList.push(obj);
        },
        /** 用户信息删除按钮操作 */
        handleDeleteSysUser() {
            if (this.checkedSysUser.length == 0) {
                this.$modal.msgError("请先选择要删除的用户信息数据");
            } else {
                const sysUserList = this.sysUserList;
                const checkedSysUser = this.checkedSysUser;
                this.sysUserList = sysUserList.filter(function (item) {
                    return checkedSysUser.indexOf(item.index) == -1;
                });
            }
        },
        /** 复选框选中数据 */
        handleSysUserSelectionChange(selection) {
            this.checkedSysUser = selection.map((item) => item.index);
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "scene/scenemember/export", {
                    ...this.queryParams,
                },
                `scenemember_${new Date().getTime()}.xlsx`
            );
        },
    },
};
</script>
